import * as requestService from './request';

function filterResult(result = {}) {
  const {success = false, data = {}, message=''} = result
  const {user, active} = data

  if(success) {
	  return {user: user, active: active};
  } else {
    return {error: message};
  }
}

export function signin(loginname, password) {
  return requestService.post('/user/signin', {loginname, password}).then(filterResult);
}

export function signup(loginname, email, password, rePassword) {
  return requestService.post('/user/signup', {loginname, email, password, rePassword}).then(filterResult);
}

export function signout() {
  console.log('signout.......')
  return requestService.post('/user/signout').then((result) => {
    const {success = false} = result
    return success
  })
}

export function tryAuth() {
  return requestService.post('/user/tryAuth').then(filterResult);
}